﻿
package sci.Notification;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;


/** NotificationMonitorService.java: 通知栏消息监听，接收通知 ----- 2018-1-17 上午10:26:53 wangzhongyuan */
@SuppressLint("NewApi")
public class NotificationService extends NotificationListenerService
{
	// 在收到消息时触发
	@Override
	public void onNotificationPosted(StatusBarNotification notice)
	{
		String msg = "Notification-Posted -> " + ToString(notice);
		
		MainActivity.showText(msg);
		// Log.i("showText", msg);
	}
	
	// 在删除消息时触发
	@Override
	public void onNotificationRemoved(StatusBarNotification notice)
	{
		String msg = "Notification-Removed -> " + ToString(notice);
		
		MainActivity.showText(msg);
		// Log.i("showText", msg);
	}
	
	// 转化为字符串
	private String ToString(StatusBarNotification notice)
	{
		Bundle extras = notice.getNotification().extras;
		
		String PackageName = notice.getPackageName();							// 获取消息APP的包名
		String notificationTitle = extras.getString(Notification.EXTRA_TITLE);	// 获取消息标题
		String notificationText = extras.getString(Notification.EXTRA_TEXT);	// 获取消息内容
		
		String Str = PackageName + " & " + notificationTitle + " & " + notificationText;
		
		return Str;
	}
	
}
